<?php
/**
 * 使用示例
 * @author M2
 * @version 1.0
 * create Date 2013-11-1
 * <yl:include file="index.html"></yl:include>
 * <yl:include file="../index.html"></yl:include>
 * <yl:include file="./index.html"></yl:include>
 */
class YlIncludeTaglib {
	
	public function __construct() {
		C(include SITE_PATH.'/extends/taglib/tagarr.php');
	}
	
	public function parse($file,$content,$templatePath) {
        if (!empty($file)) {
            $content = file_get_contents($file);
        }
        $matches = preg_parse($content, 'yl:include');
        foreach ($matches as $match) {
            $listFile = getParseOneAttr($match, 'file');
            if(substr($listFile,0,3)=='../') {
                $filename = str_ireplace('../',$templatePath,$listFile);
            } elseif(substr($listFile,0,3)<>'../' && (substr($listFile,0,2)=='./' || substr($listFile,0,2)=='~/')) {
                $filename = SITE_PATH.substr($listFile,1,strlen($listFile));
            } else {
            	$listFile = str_replace("@/", "/", $listFile);
                $filename = $templatePath.$listFile;
            }
            $filename = iconv("UTF-8","GB2312",$filename);
            if(file_exists($filename)) {
                $parseStr  = read_file($filename);
                $text = $parseStr;
            } else {
                $text = '';
            }
            $content = str_replace($match, $text, $content);
        }
        return $content;
	}
}